<?php
namespace App\Modules\Relationship\Models;


use App\Http\Services\CommonServices;
use App\Modules\Relationship\Interfaces\ModelInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use App\Http\Model\Base;
use App\Modules\Relationship\Models\CouplingRingRecordModel;
use App\Modules\Relationship\Models\CouplingRingRecordLogModel;

class CoupleRingModel extends Base {

    protected $table = "coupling_ring";
    protected $fields = ['ring_id as ringId', 'name as ringName',
        'min_value as minValue', 'max_value', 'image','level',
        'gray_image as grayImage', 'type', 'svg',
        'gray_svg as graySvg', 'ring_desc as ringDesc', 'is_show as isShow',
        'is_clear as isClear', 'sort', 'is_deleted as isDeleted'];
    protected $tablePrefix = '';

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
        $this->tablePrefix = env('DB_PREFIX');
    }

    public function getRing ($ringId){
        $condition = array(
            "ring_id"=>$ringId,
            "is_show"=>0,
            "is_deleted"=>0
        );
        $return = $this->getOneByCondition($condition, $this->fields);
        if (!$return) {
            return null;
        }
        return $return;
    }

    // 查出全部戒指
    public function getAllRings () {
        $condition = array(
            "is_deleted"=>0,
            "is_show"=>0
        );
        $rings = $this->getListByCondition($condition, $this->fields);

        return $rings;
    }


    // 获取等级对应的戒指
    public function getRingByLevel ($level) {
        $condition = array(
            "is_deleted"=>0,
            "is_show"=>0,
            "level"=>$level
        );
        $rings = $this->getListByCondition($condition, $this->fields);

        return self::objToArray($rings);
    }

    public function getRingByIds ($ids) {
        $rows = DB::table($this->table)
            ->whereIn('ring_id', $ids)
            ->where('is_deleted', 0)
            ->where('is_show', 0)
            ->select($this->fields)
            ->orderBy('ring_id')
            ->get();
        return self::objToArray($rows);
    }

    public function getRingByValue ($intimate) {
        $table = $this->tablePrefix . $this->table;
        $fields = implode(',', $this->fields);
        $sql = "select {$fields} from {$table} where min_value <= ? and type in (0,2) and is_show=0 and is_deleted=0";
        $bindings = array($intimate);
        $rows = DB::select($sql, $bindings);
        if (!$rows) {
            return [];
        }
        return self::objToArray($rows);
    }

    // 创建戒指
    public function createRing ($data) {
        $insert['name'] = (string)$data['name'];
        $insert['image'] = (string)$data['image'];
        $insert['gray_image'] = (string)$data['grayImage'];
        $insert['type'] = (int)$data['type'];
        $insert['svg'] = (string)$data['svg'];
        $insert['gray_svg'] = (string)$data['graySvg'];
        $insert['ring_desc'] = (string)$data['ringDesc'];
        $insert['level'] = (int)$data['level'];
        $insert['is_show'] = (int)$data['isShow'];
        $insert['is_clear'] = (int)$data['isClear'];
        $insert['sort'] = (int)$data['sort'];
        $insert['min_value'] = (int)$data['min_value'];
        $insert['max_value'] = (int)$data['max_value'];
        return DB::table($this->table)->insert($insert);
    }

    // 修改戒指
    public function updateRing ($ringId, $data) {
        $update['name'] = (string)$data['name'];
        $update['image'] = (string)$data['image'];
        $update['gray_image'] = (string)$data['grayImage'];
        $update['type'] = (int)$data['type'];
        $update['svg'] = (string)$data['svg'];
        $update['gray_svg'] = (string)$data['graySvg'];
        $update['ring_desc'] = (string)$data['ringDesc'];
        $update['level'] = (int)$data['level'];
        $update['is_show'] = (int)$data['isShow'];
        $update['is_clear'] = (int)$data['isClear'];
        $update['sort'] = (int)$data['sort'];
        $update['min_value'] = (int)$data['min_value'];
        $update['max_value'] = (int)$data['max_value'];
        return DB::table($this->table)->where('ring_id', $ringId)->update($update);
    }

}

